package com.ctgu.j08.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ctgu.j08.common.enums.ResultEnum;
import com.ctgu.j08.mapper.DUserMapper;
import com.ctgu.j08.mapper.DepartmentMapper;
import com.ctgu.j08.service.DepartmentService;
import com.ctgu.j08.utils.ResultUtil;
import com.ctgu.j08.vo.DUser;
import com.ctgu.j08.vo.Department;
import com.ctgu.j08.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @version 1.0
 * @author: Saiya Wang
 * @date: 2021/5/28 20:26
 */
@Service
public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Department> implements DepartmentService {
    @Autowired
    DepartmentMapper departmentMapper;
    @Override
    public Department getById(int departId) {
        Department department = departmentMapper.selectByPrimaryKey(departId);
        return department;
    }

    @Override
    public Department getByDepartName(String departName) {
        Department department = departmentMapper.selectByDepartName(departName);
        return department;
    }

    @Override
    public Result editDepart(Department department) {
        int isEdit = departmentMapper.updateByPrimaryKey(department);
        if(isEdit != 0)
        {
            return ResultUtil.success(ResultEnum.SUCCESS);
        }else
        {
            System.out.println("更新失败"+isEdit);
            return ResultUtil.error(ResultEnum.FAIL);
        }
    }

    @Override
    public Result addDepartment(Department department) {
        boolean isAdd = this.save(department);
        if(isAdd != true)
        {
            return ResultUtil.error(ResultEnum.FAIL);
        }
        else
        {
            return ResultUtil.success(ResultEnum.SUCCESS);
        }
    }

    @Override
    public Result delDepartById(Integer departId) {
        int isDel = departmentMapper.deleteByPrimaryKey(departId);
//        boolean isDel = this.removeById(userId);
        if(isDel!=0)
        {
            System.out.println("删除成功："+departId);
            return ResultUtil.success(ResultEnum.SUCCESS);
        }else
        {
            return ResultUtil.error(ResultEnum.FAIL);
        }
    }

    @Override
    public Result getAllDepartInfo() {
        List<Department> departments = departmentMapper.getAllDepartInfo();
        if(departments == null)
        {
            return ResultUtil.error(ResultEnum.FAIL);
        }else
        {
            System.out.println("部门列表："+departments);
            return ResultUtil.success(departments);
        }
    }

    @Override
    public Result searchDepartByName(String departName) {
        Department department = departmentMapper.selectByDepartName(departName);
        if(department != null)
        {
            System.out.println("搜索到的用户"+department);
            return ResultUtil.success(department);
        }
        else
        {
            return ResultUtil.error(ResultEnum.FAIL);
        }
    }
}
